package com.szy.news.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;




import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class PhotoActivity extends Activity {

	private int one;
	private int pre;
	private ImageView ivTitle;
	private ListView listview_1;
	private ListView listview_2;
	private ListView listview_3;
	private ImageButton back;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		back = (ImageButton) findViewById(R.id.photo_back);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent_back = new Intent(PhotoActivity.this,MainActivity.class);
				startActivity(intent_back);
				finish();
			}
		});
		//初始化ImageView
		ivTitle = (ImageView) findViewById(R.id.iv_title);
		//获得屏幕宽度
		Display display = getWindowManager().getDefaultDisplay();
		DisplayMetrics outMetrics = new DisplayMetrics();
		display.getMetrics(outMetrics);
		int displayWidth = outMetrics.widthPixels;//屏幕的宽度
		//图片的宽度
		Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.hoz_line);
		int imgWidth = bm.getWidth();
		int dx = (displayWidth / 3 - imgWidth) / 2;//默认位置
		Matrix matrix = new Matrix();
		matrix.postTranslate(dx, 0);
		ivTitle.setImageMatrix(matrix);
		//标题1所在位置
		one = displayWidth / 3;
		//定义ViewPager显示的页面
		List<View> views = new ArrayList<View>();
		LayoutInflater inflater = LayoutInflater.from(this);
		
		View view1 = inflater.inflate(R.layout.viewpager_pager1, null);
		listview_1 = (ListView) view1.findViewById(R.id.listView2); 
		
		List<News> data1 = new ArrayList<News>();
		data1.add(new News("知道什么是“中国好儿子”吗！","男童开车接醉酒父亲",10,R.drawable.image1_1_0));
		data1.add(new News("法国里昂，当地民众为恐怖袭击遇难者烛光守夜","巴黎，法国上万民众集会，悼念恐怖袭击遇难者",10,R.drawable.image1_2_0));
		data1.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image1_3_0));
		data1.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image1_1_0));
		data1.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image1_2_0));
		data1.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image1_3_0));
		data1.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image1_1_0));
	
		NewsAdapter adapter_listview1 = new NewsAdapter(data1);
		listview_1.setAdapter(adapter_listview1);
		
		listview_1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position<=2){
					Intent intent = new Intent(PhotoActivity.this,ListViewActivity.class);
					String viewpager1="viewpager1";
					String itemid=String.valueOf(position)+viewpager1;
					intent.putExtra(itemid, itemid);
					startActivity(intent);
				}else{
					Toast.makeText(PhotoActivity.this, "没有新闻资源", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		
		views.add(view1);
//		views.add(listview_1);
		
		View view2 = inflater.inflate(R.layout.viewpager_pager2, null);
		listview_2 = (ListView) view2.findViewById(R.id.listView3); 
		List<News> data2 = new ArrayList<News>();
		data2.add(new News("中国人的一天","图为毛浩正在抓紧时间休息",10,R.drawable.image2_1_0));
		data2.add(new News("六元旅店住客","这是生活，也是无奈",10,R.drawable.image2_2_0));
		data2.add(new News("小学生攀爬20楼高层吃饭累哭","刘建伟律师表示：这不是高不高的问题，这明显是不合理的",10,R.drawable.image2_3_0));
		data2.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image2_1_0));
		data2.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image2_2_0));
		data2.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image2_3_0));
		data2.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image2_1_0));
	
		NewsAdapter adapter_listview2 = new NewsAdapter(data2);
		listview_2.setAdapter(adapter_listview2);
		
		listview_2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position<=2){
					Intent intent = new Intent(PhotoActivity.this,ListViewActivity.class);
					String viewpager2="viewpager2";
					String itemid=String.valueOf(position)+viewpager2;
					intent.putExtra(itemid, itemid);
	//				Toast.makeText(MainActivity.this, itemid, 1000).show();
					startActivity(intent);
				}else{
					Toast.makeText(PhotoActivity.this, "没有新闻资源", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		
		views.add(view2);
		
		View view3 = inflater.inflate(R.layout.viewpager_pager3, null);
		listview_3 = (ListView) view3.findViewById(R.id.listView4); 
		List<News> data3 = new ArrayList<News>();
		data3.add(new News("一女子上班途中在单位院内倒车时，由于观察不慎，连人带车坠下6米高台","图为坠车现场",10,R.drawable.image3_1_0));
		data3.add(new News("近日，湖南长沙一所高校学生在平安夜当天，身穿汉服、手举“抵制圣诞，中国人不过外国节”的标语","看似是在保护民族文化，其实是对我们民族文化的极度不自信",10,R.drawable.image3_2_0));
		data3.add(new News("12月17日，美国总统奥巴马与古巴领袖卡斯特罗通过讲话开启了美古关系正常化进程","“破冰”的计划最大的阻力来自于古巴裔美国人——这200余万背对祖国的古巴人，有着相似的逃亡经历，也有着相似的故土情绪",10,R.drawable.image3_3_0));
		data3.add(new News("中国多地出现奶农倒奶杀牛事件","",10,R.drawable.image3_4_0));
		data3.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image3_2_0));
		data3.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image3_3_0));
		data3.add(new News("只有耐得住寂寞，才能守得住繁华","博物馆才是你该呆的地方",10,R.drawable.image3_1_0));
	
		NewsAdapter adapter_listview3 = new NewsAdapter(data3);
		listview_3.setAdapter(adapter_listview3);
		
		listview_3.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position<=2){
					Intent intent = new Intent(PhotoActivity.this,ListViewActivity.class);
					String viewpager3="viewpager3";
					String itemid=String.valueOf(position)+viewpager3;
					intent.putExtra(itemid, itemid);
					startActivity(intent);
				}else{
					Toast.makeText(PhotoActivity.this, "没有新闻资源", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		views.add(view3);
		
//		View listview1 = inflater.inflate(R.layout.viewpager_pager1,null);
//		listview_1 = (ListView) listview1.findViewById(R.id.listView2); 
		
		//设置ViewPager adapter
		ViewPager vp = (ViewPager) findViewById(R.id.vp_type);
		PagerAdapterType adapter = new PagerAdapterType(views);
		vp.setAdapter(adapter);
		//监听ViewPager事件
		vp.setOnPageChangeListener(new OnPageChangeListener(){


			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
			}

			@Override
			public void onPageSelected(int position) {
				//位置变化动画
				//RotateAnimation ScaleAnimation AlphaAnimation
				Animation anim = new TranslateAnimation(one * pre, one * position, 0, 0);
				pre = position;
				anim.setDuration(500);
				anim.setFillAfter(true);//保持动画最终状态
				ivTitle.startAnimation(anim);
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});
	}
class NewsAdapter extends BaseAdapter{
		
		private List<News> data;
		
		public NewsAdapter(List<News> data){
			this.data = data;
		}
		@Override
		public int getCount() {
			return data.size();
		}
	
		@Override
		public Object getItem(int position) {
			return data.get(position);
		}
	
		@Override
		public long getItemId(int position) {
			return position;
		}
	
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			News news = data.get(position);
			if(convertView == null){
				LayoutInflater inflater  = LayoutInflater.from(PhotoActivity.this);
				convertView = inflater.inflate(R.layout.listview_item_news, null);
			}  
			TextView tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			TextView tvDesc = (TextView) convertView.findViewById(R.id.tv_desc);
			ImageView ivPic = (ImageView) convertView.findViewById(R.id.iv_newspic);
			//设置控件的数据
			tvTitle.setText(news.getTitle());
			tvDesc.setText(news.getDesc());
			ivPic.setImageResource(news.getResId());
			return convertView;
		}

	}
	class PagerAdapterType extends PagerAdapter{
		private List<View> views;
		
		public PagerAdapterType(List<View> views){
			this.views = views;
		}
		
		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = views.get(position);
			container.addView(view);
			return view;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			View view = views.get(position);
			container.removeView(view);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
